<?php

class Search_IndexController extends Ac_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->_forward('regular');
    }

    
    public function regularAction()
    {
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        if (!empty($_GET['view']) && $_GET['view'] == 'list') {
            $per_page  = 28;
        } else {
            $per_page  = 30;
        }
        
        $res = $this->prepareSearch($page, $per_page);
        extract($res);
        
        if (DISPLAY_DEBUG) {
            echo '<pre>';
                
            print_r($_GET);
            echo '</pre>';
            //exit;
        }

        if (!empty($_GET['keyword']) && Ac_Util::checkBadWord($_GET['keyword'])) {
            $this->view->disclamer = 1;
        }        
/*
        $form = $classname::getRefineSearchForm(array('initialize' => false));
        $form->populate($_GET);
        $form->init();
 */
        //load bookmark
        $list_bmk = Model_Bookmark::getList();
        $bookmark = array();
        if (!empty($list_bmk)) {
            foreach($list_bmk as $bmk) {
                if (!empty($bmk->listing)) {
                    $bookmark[] = (string) $bmk->listing->_id;
                }
            }
        }
        $this->view->bookmark = $bookmark;        

        $this->view->query = $query;

        $this->view->item_partial   = $classname::getSearchResultPartialName();
        $this->view->sort_by        = $classname::getSearchResultSort();
        $this->view->list           = $list->response->docs;
        $this->view->listing_number = $list->response->numFound;
        $this->view->all_listing_count = $all_listing_count;
        $this->view->biz_listing_count = $biz_listing_count;
        $this->view->per_listing_count = $per_listing_count;
        $this->view->page           = $page;
        $this->view->req            = $this->_request;
        $this->view->per_page       = $per_page;

        $facet_queries = array();
        foreach($facet_counts->facet_queries as $name => $num){ 
            $arr = array();
            $tmp  = explode(':', $name);
            $name = $tmp[0];
            if (!empty($facet_queries[$name])) {
                $arr = $facet_queries[$name];
            }
            $arr[] = array('range' => $tmp[1], 'count' => $num);
            $facet_queries[$name] = $arr;
        }

        $this->view->facet_counts       = $facet_counts;
        $this->view->facet_queries      = $facet_queries;
		/*if ($_GET['small_category_list'] == 'residential' || $_GET['small_category_list'] == 'commercial') {
			$m_id = new MongoId('4cda3e818835c16810000000');
		} elseif ($_GET['small_category_list'] == 'car' || $_GET['small_category_list'] == 'boat' || $_GET['small_category_list'] == 'motorcycle') {
			$m_id = new MongoId('4cda3e818835c16810610100');
		} else {
			$m_id = new MongoId($_GET['small_category_list']);
		}
		$subcategory = Model_Category::one(array('_id' => $m_id, 'status' => Model_Category::STATUS_ACTIVE, 'path' => array('$size' => (1))));
		$this->view->subcategory = $subcategory;
		if (!empty($_GET['keyword']) && !empty($_GET['small_category_list'])) {
            $title = ucfirst(mb_substr($_GET['keyword'], 0, 30));
			$title .= ' - ' . $subcategory->name;
        }
		
		if (!empty($_GET['district'])) {
			 $title = ucfirst(Model_Location::find($_GET['district'])->name);
			 $title .= ' - ' . $subcategory->name;
		}
         */
        if (!empty($title)) {
			Zend_Registry::set('page_title', (array)$title);
            //$this->addTitle($title);
        } 
        /*
        $facet = $list->facet_counts;
        if (isset($facet)) {
            $this->view->facet          = $facet->facet_fields;
        }
         *
         */
        //$this->view->form           = $form->setAction($this->view->geturl('regular', 'search'))->setMethod('get');
        
        $search = new Model_search();
        $search->url = $_SERVER["REQUEST_URI"];
        $search->get = $_GET;
        $search->user = !empty($this->loggedin_user) ? $this->loggedin_user->_id : '';
        $search->save();
        

        //get canonical page ready
        $get = $_GET;
        if (empty($get)) {
            $get = array(1 => 1);
        }
        
        if (!Zend_Registry::isRegistered('canonical')) {
            unset($get['page']);
            $this->setCanonical($this->view->geturl('regular', 'search', array('lang' => $this->current_language), null, $get, true));
        }
        
        $this->view->search_id = $search->_id;
        if (!empty($_GET['view']) && $_GET['view'] == 'list') {
            $this->render('result-list');
        } else {
            $this->render('result');
        }
    }

    public function getCounterAction()
    {
        $page      = 0;
        $per_page  = 0;
        try {
            $res = $this->prepareSearch($page, $per_page, true);
            extract($res);
            echo (int) $list->response->numFound;
            exit;
        } catch (Exception $e) {
            if (DISPLAY_DEBUG) {
                echo $classname;
                print_r($additionalParameters);
                echo 'error';
            }
        }        
    }

    public function getAttributeAction()
    {
        $class    = $_GET['c'];
        $function = $_GET['f'];

        //Class Cars
        if ($class == 'car' && $function == 'model') {
            $list = Model_Listing_Car::getModel($_GET['id']);
        }
        if ($class == 'car' && $function == 'postad') {
            $list = Model_Listing_Car::getModelPostAd($_GET['id']);
        }
        //Class Motorcycles
        if ($class == 'motorcycle' && $function == 'model') {
            $list = Model_Listing_Motorcycle::getModel($_GET['id']);
        }
        if ($class == 'motorcycle' && $function == 'postad') {
            $list = Model_Listing_Motorcycle::getModelPostAd($_GET['id']);
        }
        //Class Boats : Boat Type/Boat Category
        if ($class == 'boattype' && $function == 'model') {
            $list = Model_Listing_Boat::getBoatCategory($_GET['id']);
        }
        if ($class == 'boattype' && $function == 'postad') {
            $list = Model_Listing_Boat::getBoatCategoryPostAd($_GET['id']);
        }

        //Class Jobs
        if ($class == 'job' && $function == 'model') {
            $list = Model_Listing_Job::getJobFunctionPostAd($_GET['id']);
        }
        if ($class == 'job1' && $function == 'model') {
            $list = Model_Listing_Job::getJobFunction($_GET['id']);
        }
        //Class Location
        if ($class == 'area' && $function == 'area_l2') {
            $list = Model_Location::getAreaL2($_GET['id']);
        }
        //Class Location bis
        if ($class == 'area' && $function == 'area_l2_bis') {
            $list = Model_Location::getAreaL2Search($_GET['id']);
        }
        //Class Location Post Ad
        if ($class == 'area' && $function == 'area_l2_postad') {
            $list = Model_Location::getAreaL2PostAd($_GET['id']);
        }
        //Class Location Post Ad
        if ($class == 'dh' && $function == 'language') {
            $list = Model_Listing_Maid::getLanguagesPostAd();
            $lng  = $list;
            $list = array();
            
            if (!empty($_GET['term'])) {
                foreach($lng  as $k => $v) {
                    if (stripos($v, $_GET['term']) !== false) {
                        $list[] = array('value' => $k, 'label' => $v);
                    }
                }
            }
        }        
        $view = $this->view;
        //Class Boats : Make
        if ($class == 'boats' && $function == 'model') {
            $list = Model_Listing_Boat::getBoats($_GET['term']);
            Zend_Controller_Action_HelperBroker::addHelper(
                new ZendX_JQuery_Controller_Action_Helper_AutoComplete()
            );
            
            if ($this->current_language != 'en') {
                foreach($list as $k => $v) {
                    $list[$k] = $view->t($v);
                }
            }
            
            $this->_helper->json($list);
        } else {
            if ($this->current_language != 'en') {
                foreach($list as $k => $v) {
                    $list[$k] = $view->t($v);
                }
            }
            
            $this->_helper->json($list);
        }
    }
    
    private function prepareSearch($page, $per_page, $quiet = false)
    {
        //echo '<pre>';print_r($_GET);exit;

        //var_dump($this->_title);exit;
        $get = $_GET;
        if(empty($get['keyword'])) {
            $get['keyword'] = '';
        } else {
            /*
            $get['keyword'] = str_replace(' ', '+', $get['keyword']).' '.$get['keyword'];
            $get['keyword_boost'] = '"'.str_replace(' ', ' ', $_GET['keyword']).'"^3';
             *
             */
        }
        
        $get['keyword'] = !empty($_GET['keyword']) ? urldecode($_GET['keyword']) : '';
        $get['keyword'] = str_replace('-', ' ', $get['keyword']);
        
        $category = '';
        //print_r($_GET['category_list']);exit;
        $get['category_list'] = empty($get['small_category_list']) ? '' :$get['small_category_list'];
        $main_cat = false;
        if (Zend_Registry::isRegistered('category_list_id')) {
            $id  = Zend_Registry::get('category_list_id');            
            $cat = Model_Category::find($id);
            $this->view->category = $cat;
            if (empty($cat->customization)) {
                $get['small_category_list'] = $id;
            }
            else {
                $get['small_category_list'] = $cat->customization;
            }            
            $get['category_list'] = $cat->customization;
            $main_cat = $cat->_id;
            $category = (string) $id;
            $_GET['small_category_list'] = $get['small_category_list'];
        }
        
        if (!empty($_GET['small_category_list'])) {
            $cat = Model_Category::find($_GET['small_category_list']);
            if (!empty($cat)) {
                $category = (string) $cat->_id;
                $main_cat = $cat->_id;
                if (empty($this->view->category)) {
                    //$this->view->category = $cat;
                }
            } elseif (empty($this->view->category)) {
                $test = $_GET['small_category_list'];
                if ($test == 'domestic-help') {
                    $test = 'maid';
                }
                //$cat = Model_Category::one(array('customization' => $test, 'path' => array('$size' => 3)));
                $cat = Model_Category::one(array('customization' => $test, 'path' => array('$size' => 3)));
                if ($cat === null) {
                    $cat = Model_Category::one(array('customization' => $test, 'path' => array('$size' => 2)));
                }
                if (!empty($cat) && $parent= $cat->getParent()) {
                    //$category = (string) $cat->_id;
                    $main_cat = $cat->path[0];
                    $this->view->category = $parent;
                    $get['category_list'] = $cat->_id;
                }
            }
        }
        
        $solr = Ac_Search::getClient();

        $get['sort'] = !empty($get['sort']) ? $get['sort'] : '';
        $classname = Model_Search::getListingInstance($get['category_list']);
		
		$level2 = $this->_request->getParam('level2');
		if (!empty($level2) && ($level2 == 'apartments-houses-for-rent' || $level2 == 'apartments-houses-for-sale')){
			$classname = 'Model_Listing_Residential';
		}
		
		if (!empty($get['category_attached']) && $get['category_attached'] == '4e40b6e19ad3e56dd6c31a1a'){
			$classname = 'Model_Listing_Car';
		}
		if (!empty($get['category_attached']) && $get['category_attached'] == '4e40b6e19ad3e56dd6c31a1d'){
			$classname = 'Model_Listing_Boat';
		}
		if (!empty($get['category_attached']) && $get['category_attached'] == '4e40b6e19ad3e56dd6c31a1e'){
			$classname = 'Model_Listing_Motorcycle';
		}
		if (!empty($get['category_attached']) && $get['category_attached'] == '4e40b5d59ad3e56dd6c319ee'){
			$classname = 'Model_Listing_Residential';
		}
		if (!empty($get['category_attached']) && $get['category_attached'] == '4e40b6289ad3e56dd6c319f2'){
			$classname = 'Model_Listing_Residential';
		}
		if (!empty($get['category_attached']) && $get['category_attached'] == '4e40b6c79ad3e56dd6c319f3'){
			$classname = 'Model_Listing_Commercial';
		}
		if (!empty($get['category_attached']) && $get['category_attached'] == '4e40b5d59ad3e56dd6c319ed'){
			$classname = 'Model_Listing_Residential';
		}
		
        //print_r($_GET['category_list']);exit;
        //echo $get['keyword'];exit;
        //$listing   = new $classname;
        $classname::processRefineSearchForm($get, $page, $per_page, $query, $fields, $additionalParameters);
        $classname::getFacetedSearch($additionalParameters);
        $areal1 = $areal2 = $catl1 = $catl2 = $keyword = '';
        $title_format = '[city][areal1][areal2][catl1][catl2][keyword]'; //([city][areal1][catl1]|catl2) - (additional terms)
       
       
        if (!empty($main_cat)) {
            //echo 'cas1';exit;
            $cat = Model_Category::find($main_cat);
            
            $main_cat = Model_Category::find($cat->path[0]);
            if (!Zend_Registry::isRegistered('page_title')) {            
                //$title = $main_cat->page_title;
                $catl1 = $main_cat->page_title;
            }
            $description = $cat->description;
            if (!empty($description)) {
                $this->view->headMeta()->appendName('description', substr($description, 0, 100));
            }
            if (!empty($main_cat)) {
                $catl1 = $main_cat->page_title;
                if (empty($catl1)) {
                    $catl1 = $main_cat->name;
                }
                $main_cat = $main_cat->_id;
            }
            //var_dump($main_cat);exit;
        } elseif (!empty($this->view->category)) {
            //echo 'cas2';exit;
            $main_cat = $this->view->category->path[0];
            $list = $classname::one(array('ca' => $this->view->category->_id));
            //$title = $list->category[0]->page_title;
            $catl1 = $list->category[0]->page_title; 
            $this->view->headMeta()->appendName('description', substr($list->category[0]->description, 0, 100));
        } elseif (!empty($_GET['small_category_list'])) {
           // echo 'cas3';exit;
            $main_cat = new MongoId($_GET['small_category_list']);
            $catl1 = Model_Category::find($main_cat)->page_title;
        }

        if (!empty($_GET['category_attached'])) {
            $cat = Model_Category::find($_GET['category_attached']);
            if (!empty($cat->page_title)) {
                $catl2 = '[city][areal1][areal2]' . $cat->page_title;
            } elseif(!empty($cat->name)) {
                $catl2 = '|[city][areal1][areal2]' . $cat->name;
            }
        }
        
        if (empty($title) && !empty($_GET['keyword'])) {
             $keyword = mb_substr($_GET['keyword'], 0, 30);
        }


        if (!empty($_GET['page'])) {
            //$title = $this->view->t('page __1__', null, $page) . (!empty($title) ? ' - '.$title : $title);
        }

        if (!empty($_GET['sort'])) {
            $arr = $classname::getSearchResultSort();
            $sort = $this->view->t($arr[$_GET['sort']]);
            //$title = ucfirst($sort)  . (!empty($title) ? ' - '.$title : $title);
        }

        if(!empty($_GET['area_l2'])) {
            $loc = Model_Location::find($_GET['area_l2']);
            $areal2 = $loc->name;
            $areal1 = $loc->area_l1->name;
        } elseif(!empty($_GET['area_l1'])) {
            $loc = Model_Location::find($_GET['area_l1']);
            $areal1 = $loc->name;
        }

/*
        $this->getFacettedTerms($additionalParameters, $main_cat);

        if (!empty($additionalParameters['facet.field'])) {
            foreach($_GET as $k => $v) {
                if (in_array($k, $additionalParameters['facet.field']) {
                    
                }
            }
        }
*/
        //if (!empty($title)) {
            $title = str_replace('[catl1]', $catl1, $title_format);
            $title = str_replace('[catl2]', $catl2, $title);
            $title = str_replace('[city]', CURRENT_CITY_NAME, $title);
            $title = str_replace('[areal1]', $areal1, $title);
            $title = str_replace('[areal2]', $areal2, $title);
            $title = str_replace('[keyword]', $keyword, $title);
            $this->addTitle($title);
        //}   

        /*
        if (empty($main_cat)) {
            echo 'cas4';
            //$list = $listing::one(array('ca' => array('$size' =>1)));
            //$main_cat = Model_Category::find($list->category[0]->path[0]);
        }
         * 
         */
        //var_dump($main_cat->id);exit;
        if(!empty($main_cat)) {
            Zend_Registry::set('main_category', Model_Category::find($main_cat));
        }
        
        $this->view->sort = !empty($get['sort']) ? $get['sort'] : $additionalParameters['sort'];
        if (empty($additionalParameters['fq'])) {
            $additionalParameters['fq'] = '';
        }

        
        //Only show PUBLISHED Ads
        if (($cat = Model_Category::find(new MongoId($main_cat))) && $cat->id == 'mc52') {
            unset($additionalParameters['qf']);
            $additionalParameters['fq'] = ' +status:1 +type_obj:'.  Model_Company::TYPE.'  +category:' . $category.' ';
        } else {
            if (empty($additionalParameters['fq'])) {
                $additionalParameters['fq'] .= ' +status:1 +type_obj_s:Model_Listing ';
            }
            else {
                $additionalParameters['fq'] .= ' +status:1 ';
            }
            if (!empty($category)) {
                $additionalParameters['fq'] .= ' +category:' . $category;
            }            
        }
		
		if (!empty($get['category_attached'])) {
			$additionalParameters['fq'] .= ' +category_attached:' . $get['category_attached'];
		}
		
		if (!empty($get['area_l2'])) {
			$additionalParameters['fq'] .= ' +area_l2:' . $get['area_l2'];
		}
		
		if (!empty($get['bedroom'])) {
			$additionalParameters['fq'] .= ' +bedroom:' . $get['bedroom'];
		}
		
		if (!empty($get['make_car'])) {
			$additionalParameters['fq'] .= ' +make:' . $get['make_car'];
		}
		
		if (!empty($get['make_motorcycle'])) {
			$additionalParameters['fq'] .= ' +make:' . $get['make_motorcycle'];
		}
		
        if (!empty($get['ad_type'])){
            if ($get['ad_type'] == 'business'){
                    $additionalParameters['fq'] .= ' +company_id:[* TO *]';
            } else {
                    $additionalParameters['fq'] .= ' -company_id:[* TO *]';
            }
        }
		
		
        if(($main_cat=="4cda3e818835c16810e40000") && (empty($_GET['sort']) || $_GET['sort'] == 'recent')){
            $additionalParameters['sort'] = "created_at desc";
        }
		

        if (empty($query)) {
            $query = '*:*';
            $additionalParameters['q.alt'] = $query;
            unset($additionalParameters['defType']);
        }
	
    /*	
		$additionalParameters['facet'] = 'true';
        $additionalParameters['facet.mincount'] = '1';
        $additionalParameters['facet.limit'] = '10';
        if (!empty($_GET['small_category_list'])) {
            $additionalParameters['facet.field'] = array(
				'category_l1',
                'category_l2',
                'make',
                'bedroom',
                'area_l1',
                'area_l2',
            );
            $cat_id = !empty($get['category_attached']) ? $get['category_attached'] : $main_cat;
            $cat = Model_Category::find($cat_id);            
            $main_category = Model_Category::find($main_cat);
            $tmp = $cat->price_range;
            if(empty($tmp)) {
                $tmp = $main_category->price_range;
            }
            $keys = array('price' => $tmp);
            $this->view->disable_price = false;

            if ($cat->customization == 'job') {
                $additionalParameters['facet.field'][] = 'salary';
                $additionalParameters['facet.field'][] = 'salary_hour';
                $_GET['view'] = empty($_GET['view']) ? 'list' : $_GET['view'];
            } elseif($cat->customization == 'residential' || $cat->customization == 'commercial') {
                $id = (string)$cat->_id;
                $keys = array();
                if ($id == RESIDENTIAL_FOR_RENT || $id == COMMERCIAL_FOR_RENT) {
                    $cat = Model_Category::find($main_cat);
                    $keys = array('rent_price' => $cat->price_range_rent);
                } elseif($id == RESIDENTIAL_FOR_SALE || $id == COMMERCIAL_FOR_SALE) {
                    $cat = Model_Category::find($main_cat);
                    $keys = array('sale_price' => $cat->price_range_sale);
                    $_GET['view'] = empty($_GET['view']) ? 'list' : $_GET['view'];
                }
            } elseif($cat instanceof Model_Category_Property) {
                $this->view->disable_price = true;
                $_GET['view'] = empty($_GET['view']) ? 'list' : $_GET['view'];
            } elseif(in_array($cat->customization, array('car', 'motorcycle','boat'))) {
                $this->view->disable_price = true;
            } elseif ($main_category->id == 'mc52') {
                $this->view->disable_price = true;
                $_GET['view'] = 'list';
            }


            

            //echo '<pre>';var_dump($main_category->price_range);exit;

            if (!$this->view->disable_price && (empty($get['price']) || empty($get['price-1'])) && !empty($keys)) {
                $additionalParameters['facet.query'] = array();
                foreach($keys as $key => $tmp) {
                    if (!empty($tmp)) {
                        foreach ($tmp as $k => $v) {
                            $v['min'] = empty($v['min']) ? '*' : $v['min'];
                            $v['max'] = empty($v['max']) ? '*' : $v['max'];
                            $additionalParameters['facet.query'][] = $key . ':['.$v['min'].' TO '.$v['max'].']';
                        }
                    }
                }
            } elseif(!$this->view->disable_price) {
                $additionalParameters['facet.query'] = array(
                    'price:['.$get['price'].' TO '.trim($get['price-1']).']',
                );
            }
        } else {
            $additionalParameters['facet.field'] = array(
                'category_l1',
            );
        }
*/
        $this->getFacettedTerms($additionalParameters, $main_cat);
        //$additionalParameters['stats'] = 'true';
        //$additionalParameters['stats.field'] = 'price';
         if (DISPLAY_DEBUG && !$quiet) {
            echo '<pre style="clear:both">';
            var_dump($classname);
            var_dump($_GET);
            var_dump('Page: '.$page, 'Per_apge: '.$per_page, 'query:'.$query);
            var_dump($additionalParameters);
            if (!empty($additionalParameters['qf'])) {
                print_r($additionalParameters['qf']);
            }
            echo '</pre>';
        } 


        $start = ($page - 1) * $per_page;

        if (empty($get['ad_type'])) {
            $list = $solr->search($query, $start, $per_page, $additionalParameters);
            $additionalParameters['fq'] .= ' +company_id:[* TO *]';
            $list2 = $solr->search($query, $start, $per_page, $additionalParameters);
            $additionalParameters['fq'] = str_replace('+company_id:[* TO *]', '-company_id:[* TO *]', $additionalParameters['fq']);
            $list3 = $solr->search($query, $start, $per_page, $additionalParameters);
            $all_listing_count = $list->response->numFound;
            $biz_listing_count = $list2->response->numFound;
            $per_listing_count = $list3->response->numFound;
        } else {
            if ($get['ad_type'] == 'business') {
                $list = $solr->search($query, $start, $per_page, $additionalParameters);
                $additionalParameters['fq'] = str_replace('+company_id:[* TO *]', '-company_id:[* TO *]', $additionalParameters['fq']);
                $list2 = $solr->search($query, $start, $per_page, $additionalParameters);
                $additionalParameters['fq'] = str_replace('-company_id:[* TO *]', '', $additionalParameters['fq']);
                $list3 = $solr->search($query, $start, $per_page, $additionalParameters);
                $all_listing_count = $list3->response->numFound;
                $biz_listing_count = $list->response->numFound;
                $per_listing_count = $list2->response->numFound;
            } else {
                $list = $solr->search($query, $start, $per_page, $additionalParameters);
                $additionalParameters['fq'] = str_replace('-company_id:[* TO *]', '+company_id:[* TO *]', $additionalParameters['fq']);
                $list2 = $solr->search($query, $start, $per_page, $additionalParameters);
                $additionalParameters['fq'] = str_replace('+company_id:[* TO *]', '', $additionalParameters['fq']);
                $list3 = $solr->search($query, $start, $per_page, $additionalParameters);
                $all_listing_count = $list3->response->numFound;
                $biz_listing_count = $list2->response->numFound;
                $per_listing_count = $list->response->numFound;
            }
        }

        return array('list' => $list,
            'all_listing_count' => $all_listing_count,
            'biz_listing_count' => $biz_listing_count,
            'per_listing_count' => $per_listing_count,
            'query' => $query,
            'classname' => $classname,
            'facet_counts' => $list->facet_counts,
        );
    }

    protected function getFacettedTerms(&$additionalParameters, $main_cat) 
    {
       $additionalParameters['facet'] = 'true';
        $additionalParameters['facet.mincount'] = '1';
        $additionalParameters['facet.limit'] = '10';
        if (!empty($_GET['small_category_list'])) {
            $additionalParameters['facet.field'] = array(
                'category_l1',
                'category_l2',
                'make',
                'bedroom',
                'city',
                'area_l1',
                'area_l2',
            );
            $cat_id = !empty($get['category_attached']) ? $get['category_attached'] : $main_cat;
            $cat = Model_Category::find($cat_id);
            $main_category = Model_Category::find($main_cat);
            $tmp = $cat->price_range;
            if(empty($tmp)) {
                $tmp = $main_category->price_range;
            }
            $keys = array('price' => $tmp);
            $this->view->disable_price = false;
            
            if ($cat->customization == 'job') {
                $additionalParameters['facet.field'][] = 'salary';
                $additionalParameters['facet.field'][] = 'salary_hour';
                $_GET['view'] = empty($_GET['view']) ? 'list' : $_GET['view'];
            } elseif($cat->customization == 'residential' || $cat->customization == 'commercial') {
                $id = (string)$cat->_id;
                $keys = array();
                if ($id == RESIDENTIAL_FOR_RENT || $id == COMMERCIAL_FOR_RENT) {
                    $cat = Model_Category::find($main_cat);
                    $keys = array('rent_price' => $cat->price_range_rent);
                } elseif($id == RESIDENTIAL_FOR_SALE || $id == COMMERCIAL_FOR_SALE) {
                    $cat = Model_Category::find($main_cat);
                    $keys = array('sale_price' => $cat->price_range_sale);
                    $_GET['view'] = empty($_GET['view']) ? 'list' : $_GET['view'];
                }
            } elseif($cat instanceof Model_Category_Property) {
                $this->view->disable_price = true;
                $_GET['view'] = empty($_GET['view']) ? 'list' : $_GET['view'];
            } elseif(in_array($cat->customization, array('car', 'motorcycle','boat'))) {
                $this->view->disable_price = true;
            } elseif ($main_category->id == 'mc52') {
                $this->view->disable_price = true;
                $_GET['view'] = 'list';
            }




            //echo '<pre>';var_dump($main_category->price_range);exit;

            if (!$this->view->disable_price && (empty($get['price']) || empty($get['price-1'])) && !empty($keys)) {
                $additionalParameters['facet.query'] = array();
                foreach($keys as $key => $tmp) {
                    if (!empty($tmp)) {
                        foreach ($tmp as $k => $v) {
                            $v['min'] = empty($v['min']) ? '*' : $v['min'];
                            $v['max'] = empty($v['max']) ? '*' : $v['max'];
                            $additionalParameters['facet.query'][] = $key . ':['.$v['min'].' TO '.$v['max'].']';
                        }
                    }
                }
            } elseif(!$this->view->disable_price) {
                $additionalParameters['facet.query'] = array(
                    'price:['.$get['price'].' TO '.trim($get['price-1']).']',
                );
            }
        } else {
            $additionalParameters['facet.field'] = array(
                'category_l1',
            );
        }
        


    }
}
